﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;

namespace dzs.Web.Outstorage
{
    /// <summary>
    /// Handler 的摘要说明
    /// </summary>
    public class Handler : IHttpHandler,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string keyword = context.Request.QueryString["keyword"];
            if (keyword != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                // 通过JavaScriptSerializer对象的Serialize序列化为["value1","value2",...]的字符串 
                if (context.Session["login"] != null)
                {
                    DataSet ds = context.Session["login"] as DataSet;
                    string user_id = ds.Tables[0].Rows[0]["user_id"].ToString();

                    string jsonString = serializer.Serialize(GetFilteredList(keyword,user_id));
                    context.Response.Write(jsonString); // 返回客户端json格式数据  
                }
                

            }            
        }
        private string[] GetFilteredList(string keyword,string user_id)
        {
            dzs.BLL.user u = new BLL.user();
            DataSet ds = u.GetUserProject(" cguid=" + user_id + " and  projectname like '%" + keyword + "%'");


            //Maticsoft.BLL.project bll = new Maticsoft.BLL.project();
            //DataSet ds = bll.GetListname(" projectname like '%" + keyword + "%'");

            List<string> proList = new List<string>();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //此处选项一般从数据库中读取，然后循环添加给countrylist对象即可。
                proList.Add(ds.Tables[0].Rows[i]["projectname"].ToString() + "|" + ds.Tables[0].Rows[i]["proid"] + "");
            }

            return proList.ToArray();
        }

      

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}